home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Codigo / Uso de la consola / CsDateProperties / CsDateProperties.cs next >
Encoding:
Text File  |  2002-07-15  |  2.3 KB  |  91 lines

  1. //-----------------------------------------------
  2. // CsDateProperties.cs ⌐ 2001 by Charles Petzold
  3. //-----------------------------------------------
  4. using System;
  5.  
  6. class CsDateProperties
  7. {
  8.      public static void Main()
  9.      {
  10.           Date mydate = new Date();
  11.  
  12.           try
  13.           {
  14.                mydate.Month = 8;
  15.                mydate.Day   = 29;
  16.                mydate.Year  = 2001;
  17.  
  18.                Console.WriteLine("Dφa del a±o = {0}", mydate.DayOfYear);
  19.           }
  20.           catch (Exception exc)
  21.           {
  22.                Console.WriteLine(exc);
  23.           }
  24.      }         
  25. }
  26. class Date
  27. {
  28.                                                             // Campos
  29.      int year;
  30.      int month;
  31.      int day;
  32.      static int[] MonthDays = new int[] {   0,  31,  59,  90, 120, 151,
  33.                                            181, 212, 243, 273, 304, 334 };
  34.  
  35.                                                             // Propiedades
  36.      public int Year
  37.      {
  38.           set
  39.           {
  40.                if (value < 1600)
  41.                     throw new ArgumentOutOfRangeException("Year");
  42.                else
  43.                     year = value;
  44.           }
  45.           get
  46.           {
  47.                return year;
  48.           }
  49.      }
  50.      public int Month
  51.      {
  52.           set
  53.           {
  54.                if (value < 1 || value > 12)
  55.                     throw new ArgumentOutOfRangeException("Month");
  56.                else
  57.                     month = value;
  58.           }
  59.           get
  60.           {
  61.                return month;
  62.           }
  63.      }
  64.      public int Day
  65.      {
  66.           set
  67.           {
  68.                if (value < 1 || value > 31)
  69.                     throw new ArgumentOutOfRangeException("Day");
  70.                else
  71.                     day = value;
  72.           }
  73.           get
  74.           {
  75.                return day;
  76.           }
  77.      }
  78.      public int DayOfYear
  79.      {
  80.           get
  81.           {
  82.                return MonthDays[month - 1] + day + 
  83.                                    (month > 2 && IsLeapYear(year) ? 1 : 0);
  84.           }
  85.      }
  86.                                                             // MΘtodo
  87.      public static bool IsLeapYear(int year)
  88.      {
  89.           return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
  90.      }
  91. }